package com.onlinestudy.controller;

import com.onlinestudy.domain.KnowledgeBase;
import com.onlinestudy.domain.KnowledgeBaseDocument;
import com.onlinestudy.service.KnowledgeBaseService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/knowledge-base")
@CrossOrigin(origins = "*", maxAge = 3600)
public class KnowledgeBaseController {

    private final KnowledgeBaseService knowledgeBaseService;

    public KnowledgeBaseController(KnowledgeBaseService knowledgeBaseService) {
        this.knowledgeBaseService = knowledgeBaseService;
    }

    @PostMapping("/create")
    public ResponseEntity<KnowledgeBase> createKnowledgeBase(@RequestBody Map<String, Object> request) {
        Long courseId = Long.valueOf(request.get("courseId").toString());
        String name = request.get("name").toString();
        KnowledgeBase kb = knowledgeBaseService.createKnowledgeBase(courseId, name);
        return ResponseEntity.ok(kb);
    }

    @PostMapping("/{knowledgeBaseId}/upload")
    public ResponseEntity<KnowledgeBaseDocument> uploadDocument(
            @PathVariable Long knowledgeBaseId,
            @RequestParam("file") MultipartFile file) {
        KnowledgeBaseDocument doc = knowledgeBaseService.uploadDocument(knowledgeBaseId, file);
        return ResponseEntity.ok(doc);
    }

    @GetMapping("/course/{courseId}")
    public ResponseEntity<KnowledgeBase> getKnowledgeBaseByCourse(@PathVariable Long courseId) {
        return knowledgeBaseService.getKnowledgeBaseByCourse(courseId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/{knowledgeBaseId}/documents")
    public ResponseEntity<List<KnowledgeBaseDocument>> getDocuments(@PathVariable Long knowledgeBaseId) {
        List<KnowledgeBaseDocument> documents = knowledgeBaseService.getDocuments(knowledgeBaseId);
        return ResponseEntity.ok(documents);
    }

    @PostMapping("/{knowledgeBaseId}/generate-outline")
    public ResponseEntity<Map<String, Object>> generateOutline(@PathVariable Long knowledgeBaseId) {
        Map<String, Object> outline = knowledgeBaseService.generateOutline(knowledgeBaseId);
        return ResponseEntity.ok(outline);
    }

    @PostMapping("/{knowledgeBaseId}/generate-ppt")
    public ResponseEntity<Map<String, Object>> generatePPT(@PathVariable Long knowledgeBaseId) {
        Map<String, Object> ppt = knowledgeBaseService.generatePPT(knowledgeBaseId);
        return ResponseEntity.ok(ppt);
    }

    @PostMapping("/{knowledgeBaseId}/generate-exam")
    public ResponseEntity<Map<String, Object>> generateExam(@PathVariable Long knowledgeBaseId) {
        Map<String, Object> exam = knowledgeBaseService.generateExam(knowledgeBaseId);
        return ResponseEntity.ok(exam);
    }

    @PostMapping("/auto-grade")
    public ResponseEntity<Map<String, Object>> autoGrade(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = knowledgeBaseService.autoGrade(request);
        return ResponseEntity.ok(result);
    }
} 